route.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { NextRequest, NextResponse } from 'next/server';
  2. const API_URL = process.env.API_URL;
  3. export async function GET(_: NextRequest, { params }: { params: Promise<{ uuid: string }> })
  4. {
  5. const { uuid } = await params;
  6. try {
  7. const res = await fetch(`${API_URL}/api/forum/post/link/${uuid}`, {
  8. cache: 'no-store',
  9. redirect: 'manual'
  10. });
  11. // 백엔드에서 리다이렉트 응답이 오면 그대로 전달
  12. if (res.status >= 300 && res.status < 400) {
  13. const location = res.headers.get('location');
  14. if (location) {
  15. return NextResponse.redirect(location);
  16. }
  17. }
  18. if (!res.ok) {
  19. return new NextResponse(null, {
  20. status: res.status
  21. });
  22. }
  23. // JSON 응답인 경우 (URL 정보 반환)
  24. const contentType = res.headers.get('content-type') || '';
  25. if (contentType.includes('application/json')) {
  26. const data = await res.json();
  27. if (data?.data?.url) {
  28. return NextResponse.redirect(data.data.url);
  29. }
  30. }
  31. return new NextResponse(null, {
  32. status: 404
  33. });
  34. } catch {
  35. return new NextResponse(null, {
  36. status: 502
  37. });
  38. }
  39. }